//
//  ZGLBaseViewController.m
//  ZeegingLottery
//
//  Created by 李曌 on 2018/8/14.
//  Copyright © 2018年 蒂埃里. All rights reserved.
//

#import "ZGLBaseViewController.h"

@interface ZGLBaseViewController () <UIGestureRecognizerDelegate>

@end

@implementation ZGLBaseViewController
{
    id<UIGestureRecognizerDelegate> _delegate;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = ViewBackgroundColor;
    
    //  从导航栏 计算 位置
//    self.edgesForExtendedLayout = UIRectEdgeNone;
    [self setLeftBackButtonWith:nil Image:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

//左返回
- (void)setLeftBackButtonWith:(NSString *)string Image:(NSString *)image
{
    if (string){
        
        UIBarButtonItem *lefeBtn = [[UIBarButtonItem alloc]initWithTitle:string style:UIBarButtonItemStylePlain target:self action:@selector(goBack)];
        [lefeBtn setTintColor:[UIColor whiteColor]];
        
        self.navigationItem.leftBarButtonItem = lefeBtn;
        
    }else if (image){
        
        UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:image] style:UIBarButtonItemStylePlain target:self action:@selector(goBack)];
        [leftBtn setTintColor:[UIColor whiteColor]];
        
        self.navigationItem.leftBarButtonItem = leftBtn;
    }else{
        
        UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"backItemImage"] style:UIBarButtonItemStylePlain target:self action:@selector(goBack)];
        [leftBtn setTintColor:[UIColor whiteColor]];
        
        self.navigationItem.leftBarButtonItem = leftBtn;
    }
    
}



#pragma mark - Private method

- (void)goBack
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    if (self.navigationController.viewControllers.count > 1) {
        // 记录系统返回手势的代理
        _delegate = self.navigationController.interactivePopGestureRecognizer.delegate;
        // 设置系统返回手势的代理为当前控制器
        self.navigationController.interactivePopGestureRecognizer.delegate = self;
    }
    
    //页面统计埋点
    [TalkingData trackPageBegin:self.title];
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    // 设置系统返回手势的代理为我们刚进入控制器的时候记录的系统的返回手势代理
    self.navigationController.interactivePopGestureRecognizer.delegate = _delegate;
    
    //页面统计埋点
    [TalkingData trackPageEnd:self.title];
}

- (void)dealloc {
    DLog(@"%s",__func__);
}

#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    return self.navigationController.childViewControllers.count > 1;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return self.navigationController.viewControllers.count > 1;
}




@end
